﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.CloudSaver.Web.Infrastructure;

namespace Microsoft.CloudSaver.Web
{
    public class Language : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //Code for language change handling
            string lang = context.Request.QueryString["lang"] as string;            
            try
            {
                if (!string.IsNullOrEmpty(lang))
                {
                    context.Response.SetCookie(new HttpCookie("LANG", lang));
                    context.Response.Redirect(context.Request.UrlReferrer.ToString());
                }                
            }
            catch { }

            //Code for theme change handling
            string theme = context.Request.Form[context.Request.Form.AllKeys.Where(p=>p.Contains("dropThemes")).First()] as string;
            try
            {
                if (!string.IsNullOrEmpty(theme))
                {
                    CloudUtils.SetValue(context.User.Identity.Name, "Theme", theme);
                    context.Response.Redirect(context.Request.UrlReferrer.ToString());
                }                
            }
            catch { }
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}
